package com.jack.payment.controller.admin.auth;

import com.jack.payment.annotation.PagingLimit;
import com.jack.payment.constant.ResultHintConstant;
import com.jack.payment.constant.enums.OfficialTypeEnum;
import com.jack.payment.entity.common.BusinessException;
import com.jack.payment.entity.common.CommonAskParam;
import com.jack.payment.entity.common.CommonResult;
import com.jack.payment.entity.filter.RestFilter;
import com.jack.payment.entity.generate.setting.OfficialModel;
import com.jack.payment.entity.generate.setting.SettingRuleModel;
import com.jack.payment.entity.generate.user.UserModel;
import com.jack.payment.entity.result.PageVo;
import com.jack.payment.service.basic.impl.ProjectConfigServiceImpl;
import com.jack.payment.util.common.PageResultUtil;
import com.jack.payment.util.common.TimeUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Tag(name = "项目配置")
@RequestMapping("/api/v1/config")
public class ProjectConfigController {
    private Logger log = LoggerFactory.getLogger(ProjectConfigController.class);

    @Autowired
    private ProjectConfigServiceImpl projectConfigService;
    @Autowired
    private PageResultUtil pageResultUtil;

    @Operation(summary = "项目设置配置")
    @RequestMapping(value = "projectSettingList", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult projectSettingList(@RequestBody CommonAskParam data) throws BusinessException {
        UserModel getUserModel = (UserModel) data.getCommon().getUserId();
        List<SettingRuleModel> settingRuleModelList = projectConfigService.getSettingRuleList();
        return CommonResult.ok(settingRuleModelList);
    }

    @Operation(summary = "项目文案内容")
    @RequestMapping(value = "projectOfficial", method = {RequestMethod.POST})
    @ResponseBody
    @PagingLimit
    public CommonResult projectOfficial(@RequestBody CommonAskParam<RestFilter.OfficialFilter> data) throws BusinessException {
        UserModel getUserModel = (UserModel) data.getCommon().getUserId();

        RestFilter.OfficialFilter requestParam = data.getData();

        Integer resultCount = projectConfigService.getFilterOfficialCount(requestParam);
        pageResultUtil.judgeRequestCount(
                requestParam.getPage(),
                resultCount
        );
        List<OfficialModel> pageList = projectConfigService.getFilterOfficialList(requestParam);
        return CommonResult.ok(new PageVo<OfficialModel>(
                pageList,
                pageResultUtil.resultPageComputer(requestParam.getCount(), resultCount),
                requestParam.getPage() / requestParam.getCount()));
    }

    @Operation(summary = "修改项目文案内容")
    @RequestMapping(value = "updateProjectOfficialContent", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult updateProjectOfficialContent(@RequestBody CommonAskParam<OfficialModel> data)throws BusinessException{
        UserModel getUserModel = (UserModel) data.getCommon().getUserId();
        OfficialModel officialModel = data.getData();

        if (officialModel.getType() == OfficialTypeEnum.RESULT.getStat() &&
            officialModel.getStatusCode() == null) {
            log.warn("当为返回消息时, 返回状态码不能为空");
            throw new BusinessException(ResultHintConstant.BASIC_DATA_DROP);
        }
        officialModel.setUpdator(getUserModel.getId());
        officialModel.setUpdateTime(TimeUtil.getCurrentTime());

        projectConfigService.updateAppointOfficial(officialModel);
        return CommonResult.ok();
    }
}
